//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ia\fn_homed.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_grp", "_pos","_debug","_units","_list","_wp","_bldgpos","_bldgs","_pos_details","_positions","_i"];
DEFAULT_PARAM(_grp,0,grpNull);
DEFAULT_PARAM(_pos,1,[]);
DEFAULT_PARAM(_debug,2,false);

if (AND_ALSO((!isnull _grp),IS_OBJECT(_grp))) then {
	_grp = group _grp;
};
if (AND_ALSO((!isnull _grp),IS_GROUP(_grp))) then {
	if (!isServer && !(local (leader _grp))) exitWith { ERROR_1("Execution should be on server or local to the unit",_this); };
	if (IS_STRING(_pos) && {IS_MARKER(_pos)}) then {
		_pos = markerPos _pos;
	};
	if (!AND_ALSO(IS_ARRAY(_pos),(count _pos == 3))) then {
		_pos = getPos (leader _grp);
	};

	_grp setBehaviour "AWARE";

	_units = (units _grp);
	DEBUG_1("Beginning",ARR_3(_grp,_pos,_units));
	if ((count _pos != 3) || {(([0,0,0] vectorDistance _pos) < 1)} || {count _units <= 0}) exitWith {
		ERROR("Position not set or empty group");
	};

	[_grp] call FFUNC(deleteWaypoints);

	_wp = _grp addWaypoint [_pos, 10];
	_wp setWaypointType "HOLD";

	// We send units (if any are left) to building pos if any
	_bldgpos = [];
	_positions = [];
	if (count _units > 0) then {
		_bldgs = _pos nearObjects ["Building", 60];
		If (count _bldgs > 0) then {
			for "_i" from 0 to (count _bldgs - 1) do {
				// We only take the first position of each building.
				_bldarr = [];
				// Using homebrewn building pos
				_bldarr = [(_bldgs select _i),[TMF_BUILDING_POS_COMBAT,TMF_BUILDING_POS_DOOR],True, True] call FFUNC(buildingPosArray);
				DEBUG_1("Building Array",ARR_3(_grp,count _units, _bldarr));
				If (count _bldarr > 0) then {
					_bldgpos = [_bldgpos, _bldarr] call FFUNC(arrayPushStack);
				};
			};
			
			_bldgpos = _bldgpos call FFUNC(arrayRandomize);
			DEBUG_1("BuildingPos to assign and units",ARR_3(_grp, count _bldgpos, (count _units)));
			for "_i" from 0 to (count _bldgpos - 1) do {
				if ((count _units) > (count _positions)) then {
					if (count (((_bldgpos select _i) select 0) nearEntities ["Man", 1.5]) <= 0) then {
						_positions set [count _positions,(_bldgpos select _i)];
					} else {
						DEBUG_1("Position already occupied",(_bldgpos select _i));
					};
				};
			};
		};
	};
	DEBUG_1("Positions found for assignment",ARR_3(_grp, count(_positions), count(_bldgpos)));
	[_grp,_positions,["hit","fire","firednear"]] call FFUNC(stuckGroup);
	
	If (_debug) then {
		_grp spawn {
			private ["_group","_units","_marker","_marker_name"];
			sleep 5;
			_group = _this;
			_units = units _group;
			{
				_marker_name = Format["Unit%1",str(_x)];
				_marker = createMarker[_marker_name,[(getPos _x) select 0,(getPos _x) select 1]];
				_marker setMarkerShape "ICON";
				_marker_name setMarkerType "loc_Tourism";
				_marker_name setMarkerColor "ColorRed";
			} foreach _units;
		};
	};
} else {
	ERROR("Group is null");
};
TRACE("End");
TMFPROFILERSTOP;